<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>修改</title>
</head>
<body>
    <div>
        <input type="hidden" id="id">
        <!--    叫隐藏域：用户看不到，只是用来存储数据-->

        <div>
            <label>
                姓名：<input type="text" id="name">
            </label>
        </div>
        <div>
            <label>
                性别：<input type="radio" name="sex" value="1" checked>男
                <input type="radio" name="sex" value="2">女
            </label>
        </div>
    
        <div>
            <label>
                年龄：<input type="text" id="age">
            </label>
        </div>
    
        <div>
            <label>
                大学：<input type="text" id="edu">
            </label>
        </div>
    
        <div>
            <label>
                学号：<input type="text" id="no">
            </label>
        </div>
    
        <div>
            <label>
                城市：
                <select name="city" id="city">
                    <option value="1">北京</option>
                    <option value="2">福州</option>
                    <option value="3">厦门</option>
                    <option value="4">龙岩</option>
                    <option value="5">泉州</option>
                </select>
            </label>
        </div>
        <div>
            <button onclick="changeClick()">修改</button>
        </div>
    </div>
    <script src="ajax.js"></script>
    <script src="common.js"></script>
    <script>
        let id = getQueryStringByName("id");
        if (!id) {
            location.href = "/StudentAdd.html"
            }
        // else{
        //     id.innerText="name";
        // }

        document.querySelector("#id").value = id;

        function changeClick() {
               change();
        }
    
        function changeRes(jsonObj) {
            alert("修改成功");
            window.location.href = "Studentlist.html"
        }
         function change() {
            let nameId = document.getElementById("name").value;
            let sexId = document.getElementsByName("sex");
            let ageId = document.getElementById("age").value;
            let eduId = document.getElementById("edu").value;
            let cityObj = document.getElementById("city");
            let cityId = cityObj.value;
            let numId = document.getElementById("no").value;
    
            let sexStr = 1;
            for (let i = 0; i < sexId.length; i++) {
                if (sexId[i].checked) {
                    sexStr = sexId[i].value;
                }
            }

             let formData = new FormData();//表单数据对象，即键值对
             formData.append("id", id);
             formData.append("name", nameId);
             formData.append("sex", sexStr);
             formData.append("age", ageId);
             formData.append("edu", eduId);
             formData.append("city", cityId);
             formData.append("no", numId);
            ajax("student/update.php", "POST", formData, changeRes);
         }
    </script>
</body>
</html>